package top.continew.admin.webapi.jobfair;

import jakarta.annotation.Resource;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.admin.jobfair.model.query.CompanyJobfairQuery;
import top.continew.admin.jobfair.model.query.CompanyQuery;
import top.continew.admin.jobfair.model.query.JobfairQuery;
import top.continew.admin.jobfair.model.req.CompanyJobfairReq;
import top.continew.admin.jobfair.model.req.JobfairReq;
import top.continew.admin.jobfair.model.resp.CompanyJobfairResp;
import top.continew.admin.jobfair.model.resp.CompanyResp;
import top.continew.admin.jobfair.model.resp.JobfairDetailResp;
import top.continew.admin.jobfair.model.resp.JobfairResp;
import top.continew.admin.jobfair.service.CompanyJobfairService;
import top.continew.admin.jobfair.service.CompanyService;
import top.continew.admin.jobfair.service.JobfairService;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 双选会信息管理 API
 *
 * @author xiahaike
 * @since 2024/09/10 12:55
 */
@Tag(name = "双选会信息管理 API")
@RestController
@CrudRequestMapping(value = "/jobfair/conferenceInfo", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class JobfairController extends BaseController<JobfairService, JobfairResp, JobfairDetailResp, JobfairQuery, JobfairReq> {

    @Resource
    private CompanyJobfairService companyJobfairService;

    @Resource
    private CompanyService companyService;

    // 查询参会企业
    @GetMapping("/companyList/{jobfairId}")
    public R<PageResp<CompanyResp>> companyList(@PathVariable Long jobfairId) {
        // 根据中间表查询参会企业id
        CompanyJobfairQuery companyJobfairQuery = new CompanyJobfairQuery();
        companyJobfairQuery.setJobFairId(jobfairId);
        List<CompanyJobfairResp> companyJobfairResp = companyJobfairService.list(companyJobfairQuery, null);
        // 将companyJobfairResp中的companyId取出来
        List<Long> companyIds = new ArrayList<>();
        for (CompanyJobfairResp companyJobfairResp1 : companyJobfairResp) {
            companyIds.add(companyJobfairResp1.getCompanyId());
        }
        // 根据companyIds查询企业信息
        PageResp<CompanyResp> companyRespPageResp = companyService.listByIds(companyIds);
        if (companyRespPageResp != null) {
            return R.ok(companyRespPageResp);
        } else {
            return R.fail("暂无参会企业");
        }
    }

    // todo 学生报名

    // todo 企业报名
    // 查询用户是否创建了企业信息 然后再查询企业报名中间表是否有数据 如果有则返回已报名
    // 如果没有则返回未报名
    @GetMapping("/companySignUp/{jobfairId}")
    public R<Long> companySignUp(@PathVariable Long jobfairId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        CompanyQuery companyQuery = new CompanyQuery();
        companyQuery.setCreateUser(loginUser.getId());
        CompanyResp companyResp = companyService.selectOne(companyQuery);
        if (companyResp == null) {
            // 未注册企业
            return R.fail("请先注册企业后再报名");
        } else {
            // 查询企业报名中间表
            Boolean b = companyJobfairService.selectOne(companyResp.getId(), jobfairId);
            if (b) {
                CompanyJobfairReq companyJobfairReq = new CompanyJobfairReq();
                companyJobfairReq.setCompanyId(companyResp.getId());
                companyJobfairReq.setJobFairId(jobfairId);
                companyJobfairService.add(companyJobfairReq);
                return R.ok("报名成功,请添加岗位信息",companyResp.getId());
            } else {
                // 已报名
                return R.fail("您已报名，请勿重复报名");
            }
        }
    }

}